package _02;

import java.util.Arrays;

public class Solution {
    public static void printToBinary(double d) {
	double HALF = .5;
	int N = 32;
	char[] array = new char[N];
	for (int i = 0; i < N; i++) {
	    if (d >= HALF) {
		array[i] = '1';
		d -= HALF;
	    } else {
		array[i] = '0';
	    }
	    d *= 2.0;
	}
	if (d > 0.0) {
	    System.err.println("ERROR");
	} else {
	    System.out.println(Arrays.toString(array));
	}
    }
    
    public static void print2Binary2(double d) {
    	double base = .5;
    	int N = 32;
    	char[] array = new char[N];
    	for (int i = 0; i < N; i++) {
    		if (d >= base) {
    			array[i] = '1';
    			d -= base;
    		} else {
    			array[i] = '0';
    		}
    		base *= .5;
    	}
    	if (d > 0.0) {
    	    System.err.println("ERROR");
    	} else {
    	    System.out.println(Arrays.toString(array));
    	}
    }
    
    public static void main(String[] args) {
		Double d = 0.75;
//		printToBinary(d);
		print2Binary2(d);
	}
}